import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/bean/wroks/oem_auditing_product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///合同特殊设置，产品设置
class UiOemAuditingProduct {
  static Widget build(
      BuildContext context, AppHttpResult<String, String>? mDatas) {
    var data = mDatas?.OemAuditingProductList;
    List<Widget> children = [];
    children.add(UiBase.title(context, AppStrings.of(context).ProductSet,
        bottom: AppEdgeInsets.margin_bottom)); //标题
    if (!StringUtils.isEmpty(data)) {
      children.add(_table(context, data));
    }
    children.add(UiBase.itemWithCheckBox(
        context,
        AppStrings.of(context).IsUseOemAuditing,
        mDatas?.isUseOemAuditing(), (value) {
      //mDatas?.setUseOemAuditing(value);//fixme 审核不能操作是否选中的状态。
    }, false)); //启用合同特殊产品设置
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(BuildContext context, List<OemAuditingProduct>? data) {
    List<TableRow> children = [];
    //标题
    children.add(TableRow(
        //第一行，添加背景色
        decoration:
            BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
        children: [
          Center(
            child: _text(AppStrings.of(context).ProductInitName), //初始的产品名称
          ),
          Center(
            child: _text(AppStrings.of(context).ProductOldName), //当前设置的产品名称
          ),
          Center(
            child: _text(AppStrings.of(context).ProductNewName), //本次修改的产品名称
          ),
        ]));
    if (data != null && data.length > 0) {
      data.forEach((element) {
        //内容
        children.add(TableRow(children: [
          Center(
            child: _text(element.ProductInitName), //初始的产品名称
          ),
          Center(
            child: _text(element.ProductOldName), //当前设置的产品名称
          ),
          Center(
            child: _text(element.ProductNewName), //本次修改的产品名称
          ),
        ]));
      });
    }
    var table = Table(
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      textDirection: TextDirection.ltr,
      children: children,
      border: AppStyle.getTableBorder(),
    );
    var width = px.screenWidth();
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      physics: BouncingScrollPhysics(),
      child: Container(
        width: width,
        child: table,
      ),
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text ?? "",
      style: style,
    );
  }
}
